package org.michaelrenaud.pathfinder.client.core.app.character.widget;

import java.util.List;

import org.michaelrenaud.pathfinder.shared.model.Skill;
import org.michaelrenaud.pathfinder.shared.model.Stat;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;

public class SkillRow extends Composite  {
	
	interface Binder extends UiBinder<Widget, SkillRow> {}
	private static Binder binder = GWT.create(Binder.class);
	
	interface Style extends CssResource {
		String odd();
		String first();
		String checked();
		String nopenalty();
	}
	
	@UiField Style style;
	@UiField InlineLabel txtSkillName;
	@UiField HTMLPanel chkUntrained;
	@UiField HTMLPanel pnlSkillBonus;
	@UiField Label txtSkillBonus;
	@UiField Label txtSkillModifier;
	@UiField HTMLPanel chkClassSkill;
	@UiField Label txtSkillRank;
	@UiField Label txtSkillFeatBonus;
	@UiField Label txtPenaltyOperator;
	@UiField HTMLPanel pnlSkillPenalty;
	@UiField Label txtSkillPenalty;
	
	private boolean isOdd;
	private Skill skill;
	private boolean isFirstRow;

	public SkillRow() {
		initWidget(binder.createAndBindUi(this));
	}
	
	public void setOdd(boolean isOdd) {
		this.isOdd = isOdd;
		if(isOdd) {
			addStyleName(style.odd());
		}
		else {
			removeStyleName(style.odd());
		}
	}
	
	public boolean isOdd() {
		return isOdd;
	}
	
	public Skill getSkill() {
		return skill;
	}
	
	public void setFirstRow(boolean isFirstRow) {
		this.isFirstRow = isFirstRow;
		if(isFirstRow) {
			txtSkillBonus.addStyleName(style.first());
		}
		else {
			txtSkillBonus.removeStyleName(style.first());
		}
	}
	
	public boolean isFirstRow() {
		return isFirstRow;
	}
	
	public void setStats(int modifier, String modifierName, boolean isClassSkill, List<String> classes, Stat featBonus, Stat penalty, int bonus) {
		if(skill.getRanks() > 0)
			txtSkillRank.setText(skill.getRanks() + "");
		else
			txtSkillRank.setText("");
		
		txtSkillModifier.setText(modifier + "");
		txtSkillModifier.setTitle(modifierName);
		if(isClassSkill) {
			chkClassSkill.addStyleName(style.checked());
			String tooltip = classes.get(0);
			for(int i = 1; i < classes.size(); i++) {
				tooltip += "\n" + classes.get(i);
			}
			chkClassSkill.setTitle(tooltip);
		}
		else
			chkClassSkill.removeStyleName(style.checked());
		
		if(featBonus.getTotal() > 0) {
			txtSkillFeatBonus.setText(featBonus.getTotal() + "");
			txtSkillFeatBonus.setTitle(featBonus.getTooltip());
		}
		else {
			txtSkillFeatBonus.setText("");
			txtSkillFeatBonus.setTitle("");
		}
		
		if(skill.getModifierName().equals("STR") || skill.getModifierName().equals("DEX")) {
			txtPenaltyOperator.setText("-");
			txtSkillPenalty.setText(penalty.getTotal() + "");
			txtSkillPenalty.setTitle(penalty.getTooltip());
		}
		
		if(skill.isUntrained() || (!skill.isUntrained() && skill.getRanks() > 0)) {
			txtSkillBonus.setText(bonus + "");
		}
		else {
			txtSkillBonus.setText("-");
		}
	}
	
	public void setSkill(Skill skill, SkillRow previousSkillRow) {
		this.skill = skill;
		txtSkillName.setText(skill.getName());
		txtSkillName.setTitle(skill.getTooltip());
		
		if(skill.isUntrained())
			chkUntrained.addStyleName(style.checked());
		else
			chkUntrained.removeStyleName(style.checked());
		
		if(previousSkillRow == null) {
			pnlSkillBonus.addStyleName(style.first());

			if(skill.getModifierName().equals("STR") || skill.getModifierName().equals("DEX")) {
				pnlSkillPenalty.addStyleName(style.first());
				pnlSkillPenalty.removeStyleName(style.nopenalty());
			}
			else {
				pnlSkillPenalty.addStyleName(style.nopenalty());
			}
		}
		else {
			Skill previousSkill = previousSkillRow.getSkill();
			
			pnlSkillBonus.removeStyleName(style.first());
			setOdd(!previousSkillRow.isOdd());
			
			if(skill.getModifierName().equals("STR") || skill.getModifierName().equals("DEX")) {
				if(previousSkill.getModifierName().equals("STR") || previousSkill.getModifierName().equals("DEX")) {
					pnlSkillPenalty.removeStyleName(style.first());
				}
				else {
					pnlSkillPenalty.addStyleName(style.first());
				}
				pnlSkillPenalty.removeStyleName(style.nopenalty());
			}
			else {
				pnlSkillPenalty.addStyleName(style.nopenalty());
			}
		}
		
	}
}
